define(['./../initial', './Project.Layer', './Project.MonitorPoints', './Project.Status'], function () {
    var app = angular.module('App');
    app.factory('Project', ['$q', '$http', 'viewbag', 'Project.Layer', 'Project.MonitorPoints', 'Project.Status',
        function ($q, $http, viewbag, Project_Layer, Project_MonitorPoints, Project_Status) {
            var Project = function () {
            };
            $.extend(Project.prototype, Project_Layer);
            $.extend(Project.prototype, Project_MonitorPoints);
            $.extend(Project.prototype, Project_Status);
            
            Project.typeList = [];//用户可以操作的项目类型 Array<obejct>
            Project.cityList = null;//当前类型下的所有城市列表,Array<string>
            /** 获取用户能访问到的所有项目类型 */
            Project.getTypes = function () {
                //List<{typeCode,typeName,markerColor}>)
                var defer = $q.defer();
                if (Project.typeList.length > 0) {
                    defer.resolve(Project.typeList);
                } else {
                    $http({
                        url: '/metro/project/authorizedProjectTypes',
                        method: 'get'
                    }).success(function (data) {
                        Project.typeList = data;
                        defer.resolve(data);
                    });
                }
                return defer.promise;
            };

            /** 
             * 获取前端所有项目,结果是城市->线路->工程的列表
             */
            Project.getAllCityProjects = function (typeCode) {
                localStorage.setItem('projectType', typeCode); //持久化到浏览器
                viewbag.curType = Project.typeList.filter(function (type) {
                    return type.typeCode === typeCode;
                })[0];
                var defer = $q.defer();
                if (Project[typeCode + 'CityList']) {
                    Project.cityList = Project[typeCode + 'CityList'];
                    viewbag.curCity = Project.cityList[0];//默认是第一个city
                    defer.resolve(Project[typeCode + 'CityList']);
                } else {
                    $http({
                        url: '/metro/project/frontprojects',
                        params: {
                            type: typeCode
                        },
                        method: 'get'
                    }).success(function (data) {
                        data.forEach(function (city) {
                            city.lines.forEach(function (line) {
                                line.projects = line.projects.map(function (prj) {
                                    return $.extend(new Project(), prj);
                                });
                                line.open = line.projects.length < 5;//是否折叠该项目
                                line.backupProjects = line.projects;
                            });
                            city.backupLines = city.lines;//备份一下lines列表,筛选后还可以恢复
                        });
                        Project[typeCode + 'CityList'] = data;
                        Project.cityList = data;
                        viewbag.curCity = Project.cityList[0];//默认是第一个city
                        defer.resolve(Project[typeCode + 'CityList']);
                    });
                }
                return defer.promise;
            };

            /** 在当前项目类型下,按照id查找项目 */
            Project.findPrjById = function (id) {
                var find = false;
                var tempPrj = null;
                Project[viewbag.curType.typeCode + 'CityList'].some(function (city) {
                    city.lines.some(function (line) {
                        line.projects.some(function (prj) {
                            find = prj.prjId === id;
                            if (find) {
                                tempPrj = prj;
                                line.open = true;
                            }
                            return find;
                        });
                        return find;
                    });
                    return find;
                });
                return tempPrj;
            };

            return Project;
        }]);
});